﻿using Aierp.SystemEntities.System;
using Aierp.SystemRepository.Models;
using Aierp.Utility.Data;
using Chloe;

namespace Aierp.SystemRepository.Implements
{
    public class NavButtonsRepository:DbInstance
    {
        /// <summary>
        /// 查询一个编码-根据ID，只返回指定信息，密码等关键信息不返回
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Sys_NavButtons FindById(int key)
        {
            return this.FindBy<Sys_NavButtons>(c => c.KeyId == key);
        }

        /// <summary>
        /// 查询一个编码-根据ID，只返回指定信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object FindByNavId(int key)
        {
            IQuery<Sys_NavButtons> navButtons = Db.Query<Sys_NavButtons>();
            IQuery<Sys_Buttons> buttons = Db.Query<Sys_Buttons>();
            IJoiningQuery<Sys_NavButtons, Sys_Buttons> view = navButtons.LeftJoin(buttons,
                (Sys_NavButtons, Sys_Buttons) => Sys_NavButtons.ButtonId == Sys_Buttons.KeyId);
            var result = view.Select((Sys_NavButtons, Sys_Buttons) => new
            {
                KeyId = Sys_NavButtons.KeyId,
                NavId = Sys_NavButtons.NavId,
                ButtonId = Sys_NavButtons.ButtonId,
                Sortnum = Sys_NavButtons.Sortnum,
                ButtonText = Sys_Buttons.ButtonText,
                ButtonTag = Sys_Buttons.ButtonTag,
                ButtonHtml = Sys_Buttons.ButtonHtml
            });

            return result.Where(c => c.NavId == key).ToList();
        }

        /// <summary>
        /// 查询一个编码-根据navId和buttonId
        /// </summary>
        /// <param name="key"></param>
        /// <param name="navId"></param>
        /// <param name="buttonId"></param>
        /// <returns></returns>
        public Sys_NavButtons FindByNavIdAndButtonId(int navId,int buttonId)
        {
            return this.FindBy<Sys_NavButtons>(c => c.NavId == navId && c.ButtonId == buttonId);
        }

        /// <summary>
        /// 获取一个列表，供给Grid使用
        /// </summary>
        /// <param name="sortOrder"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="searchTxt"></param>
        /// <returns></returns>
        public string GetList(string sortOrder, int limit, int offset, string searchTxt)
        {
            if (!string.IsNullOrEmpty(searchTxt))
            {
                int.TryParse(searchTxt, out var keyId);
                var status = EnumStatus.POST.ToString();

                return this.GetPageList<Sys_NavButtons, object>(limit, offset, c => c.NavId == keyId, c => c.Sortnum, sortOrder);
            }
            return this.GetPageList<Sys_NavButtons, object>(limit, offset, null, c => c.Sortnum, sortOrder);

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(Sys_NavButtons item)
        {
            if (item == null) return 0;

            var model = this.FindByNavIdAndButtonId(item.NavId, item.ButtonId);
            if (model != null) return -2;

            item.NavId = item.NavId;
            item.ButtonId = item.ButtonId;
            item.Sortnum = item.Sortnum;
            var ret = Db.Insert(item);

            return ret?.KeyId ?? 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int Remove(int key)
        {
            //先检查是否存在
            var model = this.FindById(key);
            if (model == null) return -2;
            return Db.DeleteByKey<Sys_NavButtons>(key);
        }

        /// <summary>
        /// 根据NavId移除
        /// </summary>
        /// <param name="navId"></param>
        /// <returns></returns>
        public int RemoveByNavId(int navId)
        {
            return Db.Delete<Sys_NavButtons>(c => c.NavId == navId);
        }
    }
}
